//
//  DYPrintCmd.m
//  DYPrintSDK
//
//  Created by mr.ye on 2023/11/20.
//

#import "DYPrintCmd.h"
#import "DYPrintParse.h"
#import <ImageDataProcesser/ImageDataProcesser.h>
@interface DYPrintCmd ()

@end

@implementation DYPrintCmd

- (instancetype)init
{
    self = [super init];
    if (self) {
      
    }
    return self;
}



//设置浓度
+ (NSData *)setThickCmd:(NSUInteger)thick {
    Byte list[] = {0x10, 0xFF, 0x10, 0x00, thick};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}


//使能打印机
+ (NSData *)printerEnabel {
    Byte list[] = {0x10, 0xFF, 0xF1, 0x03};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}


//唤醒打印机
+ (NSData *)printerWake {
    Byte list[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}


//处理图片
+ (NSData *)printImageCmd:(UIImage *)image {
    NSData *image_data = [self blackWhiteProcessing:image];
    int width = (int)image.size.width / 8;
    if ((int)image.size.width % 8 > 0) {
        width += 1;
    }
    int height = (int)image.size.height;
    
    Byte w_h = (width & 0xFF00) >> 8;
    Byte w_l = (width & 0x00FF);
    
    Byte h_h = (height & 0xFF00) >> 8;
    Byte h_l = (height & 0x00FF);
    
    Byte list[] = {0x1D, 0x76, 0x30, 0x00, w_l, w_h, h_l, h_h};
    NSMutableData *data = [NSMutableData dataWithBytes:list length:sizeof(list)];
    [data appendData:image_data];
    return data;
}

//A4压缩
+ (NSData *)compressPrintImageCmd:(UIImage *)image {
    NSData *data = [[ImageDataProcesser new] bmpToDataWithA8:image];
    return data;
}



//走纸
+ (NSData *)printerWalk:(NSUInteger)walk {
    Byte list[] = {0x1B, 0x4A, walk};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}

//状态查询
+(NSData*)stateCmd{
    Byte list[] = {0x10, 0xFF, 0x40};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}
//获取信息
+ (NSData *)getInfoCmd{
    Byte cmd[] = {0x10, 0xFF, 0x70};
    NSData *data = [NSData dataWithBytes:cmd length:sizeof(cmd)];
    return data;
}


//停止打印
+ (NSData *)printerStop {
    Byte list[] = {0x10, 0xFF, 0xF1, 0x45};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}

//设置关机时间
+ (NSData *)setShutTimeCmd:(NSUInteger)closeTime {
    Byte list[] = {0x10, 0xFF, 0x12, 0x00, closeTime};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}
//设置浓度
+ (NSData *)setColorCmd:(NSUInteger)thick {
    Byte list[] = {0x10, 0xFF, 0x10, 0x00, thick};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}

//获取关闭时间
+(NSData*)closeTimeCmd{
    Byte list[] = {0x10, 0xFF, 0x13};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}
//获取型号
+ (NSData *)getModelCmd {
    Byte list[] = {0x10, 0xFF, 0x20, 0xF0};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}
//获取浓度
+ (NSData *)getThickCmd {
    Byte list[] = {0x10, 0xFF, 0x11};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}
//出纸
+ (NSData *)printerOut {
    Byte list[] = {0x1F, 0x11, 0X51};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}

//设置纸张
+ (NSData *)setPaperTypeDataCmd:(DYPrintPaperType)type {
    Byte list[] = {0x1F, 0x80, 0x01, type};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}
//进纸
+ (NSData *)printerEnter {
    Byte list[] = {0x1F, 0x11, 0X50};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}
//定位标签缝隙
+ (NSData *)printerLocation {
    Byte list[] = {0x1D, 0x0C};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}

//定位缝隙 (标签和标准尺寸的A4)(用于：A4和P50)
+(NSData*)printerLocationA4:(DYPrintPaperType)mode{
    NSData *commandData = [NSData dataWithBytes:(unsigned char[]){0x1f, 0x12, mode, 0x01} length:4];
    return commandData;
}
#pragma mark - 升级指令
//准备升级(1)
+ (NSData *)willUpdate{
    Byte list[] = {0x10, 0xFF, 0xE0, 0xAA, 0xAA};
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}
//查询设备(2)
+ (NSData *)searchWillUpdate{
    Byte list[] = {0x1B, 0x10, 0x00, 0x06, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    Byte enc = [DYPrintParse calculateEnc:list length:sizeof(list)];
    list[sizeof(list) - 1] = enc;
    NSData *data = [NSData dataWithBytes:list length:sizeof(list)];
    return data;
}

//擦除要存储的内存(3)
+ (NSData *)cleanMemory:(NSData *)data {
    
    NSUInteger startAddress = 0x08008000;
    NSUInteger endAddress = startAddress + data.length;
    
    NSData *startData = [DYPrintParse intToByteArray4:startAddress length:4];
    NSData *endData = [DYPrintParse intToByteArray4:endAddress length:4];
    
    Byte list[] = {0x1B, 0x10, 0x00, 0x0F, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00};
    NSMutableData *res = [NSMutableData dataWithBytes:list length:sizeof(list)];
    [res appendData:startData];
    [res appendData:endData];
    
    Byte resbytes[res.length];
    [res getBytes:&resbytes length:res.length];
    Byte enc = [DYPrintParse calculateEnc:resbytes length:res.length];
    Byte encList[] = {enc};
    NSData *encData = [NSData dataWithBytes:encList length:sizeof(encList)];
    
    [res appendData:encData];
    return res;
}
//重启打印机
+ (NSData *)restartPrinter {
    Byte list[] = {0x1B, 0x10, 0x00, 0x06, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00};
    NSMutableData *data = [NSMutableData dataWithBytes:list length:sizeof(list)];
    Byte enc = [DYPrintParse calculateEnc:list length:sizeof(list)];
    [data appendBytes:&enc length:1];
    return data;
}





//图片处理
+ (NSData *)blackWhiteProcessing:(UIImage *)bitmap{
    
    int bytewidth;
    int width = bitmap.size.width;
    int height = bitmap.size.height;

    CGImageRef cgImage = [bitmap CGImage];
    NSUInteger bytesPerPixel = CGImageGetBitsPerPixel(cgImage) / 8;
    NSUInteger bytesPerRow = CGImageGetBytesPerRow(cgImage);

    // 获取位图数据
    CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
    const UInt8 *imageBytes = CFDataGetBytePtr(pixelData);

    if (width % 8 != 0)
        bytewidth = width / 8 + 1;
    else
        bytewidth = width / 8;

    NSMutableData *printData = [NSMutableData new];

    Byte *pixel = malloc(bytewidth * height);
    int comlen = 0;
    Byte temp;

    // 循环高
    for (int i = 0; i < height; i++) {
        // 循环宽度
        for (int j = 0; j < bytewidth; j++) {
            temp = 0;
            for (int k = 0; k < 8; k++) {
                if ((j * 8 + k) < width) {
                    NSUInteger pixelIndex = (i * bytesPerRow) + (j * 8 + k) * bytesPerPixel;

                    UInt8 red = imageBytes[pixelIndex];
                    UInt8 green = imageBytes[pixelIndex + 1];
                    UInt8 blue = imageBytes[pixelIndex + 2];

                    if ((0.3 * red + 0.59 * green + 0.11 * blue) < 128) {
                        temp |= (Byte)(128 >> k);
                    }
                }
            }
            pixel[comlen++] = temp;
        }
    }

    NSData *pixelData1 = [NSData dataWithBytesNoCopy:pixel length:bytewidth * height freeWhenDone:YES];
    [printData appendData:pixelData1];

    CFRelease(pixelData);

    return printData;
}


@end
